package com.hjx.windowtool.txt;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TxtProcess {
	private static final String charsetName = "UTF-8";
	//(.*)([0-9]+)([' ']+)(.*)
	public static void main(String[] args) throws Exception {
		String s = " 001 sadas";
		String regex = "(.*)([0-9]+)([' ']+)(.*)";
		System.out.println(s.matches(regex));
		Pattern p = Pattern.compile("([0-9]+)");
		Matcher m = p.matcher(s);
		if (m.find()) {
			System.out.println(m.start()+"==="+m.end()+"=="+s.substring(m.start(), m.end()));
		}
		
		String path = "C:\\Users\\hjx\\Documents\\Tencent Files\\2954775548\\FileRecv\\自完美世界开始.txt";
		File from = new File(path);
		File to = new File(from.getParent(), "1"+from.getName());
		if (!to.exists()) {
			to.createNewFile();
		}
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(to), charsetName));
		
		BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(from), charsetName));
		String readLine = br.readLine();
		while(readLine!=null) {
			if (readLine.matches("([0-9]+)")) {
				readLine=readLine+"章 ";
			}else if (readLine.matches("([0-9]+)([' ']+)(.*)")) {
				String[] split = readLine.split(" ");
				readLine=split[0]+"章"+readLine.substring(readLine.indexOf(" "));
			}
			bw.write(readLine);
			bw.newLine();
			readLine = br.readLine();
		}
	}
}
